﻿Imports Microsoft.Kinect

Class MainWindow
    Dim sensor As KinectSensor
    Dim PixelData() As Byte
    Dim Skeletons() As Skeleton

    Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        sensor = Microsoft.Kinect.KinectSensor.KinectSensors(0)

        sensor.ColorStream.Enable()
        sensor.SkeletonStream.Enable()

        AddHandler sensor.ColorFrameReady, AddressOf sensor_VideoFrameReady
        AddHandler sensor.SkeletonFrameReady, AddressOf sensor_SkeletonFrameReady

        sensor.Start()
    End Sub

    Private Sub Window_Unoaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Unloaded
        sensor.Stop()
    End Sub

    Private Sub sensor_VideoFrameReady(sender As Object, e As ColorImageFrameReadyEventArgs)
        Dim ReceivedData As Boolean = False

        Using CFrame As ColorImageFrame = e.OpenColorImageFrame()
            If CFrame Is Nothing Then
                ' The image processing took too long. More than 2 frames behind.
            Else
                ReDim PixelData(CFrame.PixelDataLength - 1)
                CFrame.CopyPixelDataTo(PixelData)
                ReceivedData = True
            End If
        End Using

        If ReceivedData Then
            Dim source As BitmapSource = BitmapSource.Create(640, 480, 96, 96,
    PixelFormats.Bgr32, Nothing, PixelData, 640 * 4)

            videoImage.Source = source
        End If


    End Sub

    Private Sub sensor_SkeletonFrameReady(sender As Object, e As SkeletonFrameReadyEventArgs)

        Dim ReceivedData As Boolean = False

        Using SFrame As SkeletonFrame = e.OpenSkeletonFrame()
            If SFrame Is Nothing Then
                ' The image processing took too long. More than 2 frames behind.
            Else
                ReDim Skeletons(SFrame.SkeletonArrayLength - 1)
                SFrame.CopySkeletonDataTo(Skeletons)
                ReceivedData = True
            End If
        End Using

        If ReceivedData Then

            Dim currentSkeleton As Skeleton = (From s In Skeletons Where s.TrackingState = SkeletonTrackingState.Tracked
            Select s).FirstOrDefault()

            If currentSkeleton IsNot Nothing Then
                SetEllipsePosition(head, currentSkeleton.Joints(JointType.Head))
                SetEllipsePosition(leftHand, currentSkeleton.Joints(JointType.HandLeft))
                SetEllipsePosition(rightHand, currentSkeleton.Joints(JointType.HandRight))
            End If
        End If
    End Sub

    Private Sub SetEllipsePosition(ByVal ellipse As Ellipse, ByVal joint As Joint)

        Dim vector As New Microsoft.Kinect.SkeletonPoint()
        vector.X = ScaleVector(640, joint.Position.X)
        vector.Y = ScaleVector(480, -joint.Position.Y)
        vector.Z = joint.Position.Z

        Dim updatedJoint As New Joint()
        updatedJoint = joint
        updatedJoint.TrackingState = JointTrackingState.Tracked
        updatedJoint.Position = vector

        Canvas.SetLeft(ellipse, updatedJoint.Position.X)
        Canvas.SetTop(ellipse, updatedJoint.Position.Y)
    End Sub


    Private Function ScaleVector(ByVal length As Integer, ByVal position As Single) As Single

        Dim value As Single
        value = (((Convert.ToSingle(length) / 1.0F) / 2.0F) * position) + (length / 2)

        If value > length Then
            Return Convert.ToSingle(length)
        End If

        If value < 0.0F Then
            Return 0.0F
        End If

        Return value
    End Function
End Class